package com.example.servlet;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/ServletDemo" ,loadOnStartup =0 )
public class ServletDemo implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
      System.out.println("ServletDemo 的init()方法执行........");
      //模拟耗时操作，这里使用线程睡5秒钟
        try {
            Thread.sleep(5*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("ServletDemo 的init()方法执行完成");
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("ServletDemo service()方法执行~~~~~~~~~~~");
        PrintWriter writer = servletResponse.getWriter();
        writer.println("Hello Servlet!!!");


    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("ServletDemo destroy()方法执行~~~~~~~~~~~");
    }
}
